package cau.se.project.data;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.swing.tree.TreeNode;

import cau.se.project.data.method.GroupControl;
import cau.se.project.data.method.RowControl;

public class DSMRow implements RowControl {
	private String m_name;
	protected List<RowControl> m_dependencyList;
	private GroupControl parent = null;
	
	public DSMRow(String name) {
		if(name == null)
			m_name = null;
		else
			m_name = new String(name);
		m_dependencyList = new ArrayList<RowControl>();
	}
	
	@Override
	public void removeDependency(RowControl row)
	{
		// 부모에도 dependency 뺌
		if(parent != null)
			parent.removeDependency(row);
		m_dependencyList.remove(row);
	}
	
	@Override
	public void setDependency(RowControl row)
	{
		if(m_dependencyList.contains(row) == false) {
			// leaf라면 부모에도 dependency 넣음
			if(parent != null)
				parent.setDependency(row);
			m_dependencyList.add(row);
		}
		else {
			System.out.println(row.getName() + " already in the list!");
		}
	}

	@Override
	public void setName(String newName) {
		m_name = new String(newName);
	}
	
	@Override
	public String getName()
	{
		return m_name;
	}

	@Override
	public TreeNode getChildAt(int childIndex) {
		return null;
	}

	@Override
	public int getChildCount() {
		return 0;
	}

	@Override
	public TreeNode getParent() {
		return parent;
	}

	@Override
	public int getIndex(TreeNode node) {
		// child가 없으므로 -1을 반환함
		return -1;
	}

	@Override
	public boolean getAllowsChildren() {
		return false;
	}

	@Override
	public boolean isLeaf() {
		return true;
	}

	@Override
	public Enumeration<TreeNode> children() {
		return null;
	}

	@Override
	public void setParent(GroupControl parent) {
		this.parent = parent;
	}

	@Override
	public List<RowControl> getDepenencyList() {
		return m_dependencyList;
	}

	@Override
	public int getDepth() {
		int ret = 0;
		RowControl row = this;
		while((row = (RowControl)row.getParent()) != null) {
			ret++;
		}
		
		return ret;
	}
}
